package com.mhkjup.artcircle.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.mhkjup.artcircle.dao.JC_ZhDao;
import com.mhkjup.artcircle.db.JC_ZhDB;
import com.mhkjup.artcircle.service.JC_ZhService;
import com.mhkjup.artcircle.util.TimeUtil;

@Service
public class JC_ZhServiceIml implements JC_ZhService {
	@Resource(name = "JC_ZhDao")
	private JC_ZhDao jcDao;

	@Override
	public Map<String, Object> registerJC_ZhDB(String Dlmc, String Dlpass,
			String ZHTel, String SyTel, String MDNM, String MDAdrr,
			HttpServletRequest request) {
		// TODO Auto-generated method stubw
		Map<String, Object> map = new HashMap<String, Object>();
		if (Dlmc == null || Dlmc.equals("")) {
			map.put("code", 400);
			map.put("msc", "账号为空");
			return map;
		}
		if (Dlpass == null || Dlpass.equals("")) {
			map.put("code", 400);
			map.put("msc", "密码为空");
			return map;
		}
		if (ZHTel == null || ZHTel.equals("")) {
			map.put("code", 400);
			map.put("msc", "手机号为空");
			return map;
		}
		if (SyTel == null || SyTel.equals("")) {
			map.put("code", 400);
			map.put("msc", "修改密码手机号为空");
			return map;
		}
		if (jcDao.getJC_ZhByDlmc(Dlmc) != null) {
			map.put("code", 401);
			map.put("msc", "用户已存在");
			return map;
		}
		// 以上为验证操作
		// ip获取操作
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		// 存入数据库操作开始
		JC_ZhDB jcdb = new JC_ZhDB();
		jcdb.setZhId(TimeUtil.getuniqukey());
		jcdb.setDlmc(Dlmc);
		jcdb.setDlpass(Dlpass);
		jcdb.setDeleteFlg("0");// 删除表示新注册用户默认为0
		jcdb.setSytel(SyTel);
		jcdb.setZhtel(ZHTel);
		jcdb.setOpId(ip);
		jcdb.setMakeDt(TimeUtil.getCurrentTimeSceond());
		jcdb.setSysStartDt(TimeUtil.getCurrentTimeSceond());
		if (jcDao.addJC_ZhDB(jcdb) == 1) {
			map.put("code", 200);
			map.put("msc", "注册成功");
			return map;
		} else {
			map.put("code", 402);
			map.put("msc", "注册失败");
			return map;
		}

	}

	@Override
	public Map<String, Object> loginJC_ZhDB(String Dlmc, String Dlpass) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		if (Dlmc == null || Dlmc.equals("")) {
			map.put("code", 400);
			map.put("msc", "账号为空");
			return map;
		}
		if (Dlpass == null || Dlpass.equals("")) {
			map.put("code", 400);
			map.put("msc", "密码为空");
			return map;
		}
		JC_ZhDB jcdb = jcDao.getJC_ZhByDlmc(Dlmc);
		if (jcdb == null) {
			map.put("code", 402);
			map.put("msc", "用户不存在");
			return map;
		}
		if (jcdb.getDeleteFlg().equals("1")) {
			map.put("code", 405);
			map.put("msc", "账户已被冻结");
			return map;
		}

		if (jcdb.getDlmc().equals(Dlmc) && jcdb.getDlpass().equals(Dlpass)) {
			map.put("code", 200);
			map.put("msc", "登录成功");
			map.put("data", Dlmc);
			return map;
		} else {
			map.put("code", 300);
			map.put("msc", "登录失败登录账号或密码错误");
			return map;

		}
	}

	@Override
	public Map<String, Object> updateUserDB(String Dlmc, String newPass,
			String ondDlpass) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (Dlmc == null || Dlmc.equals("")) {
			map.put("code", 400);
			map.put("msc", "账号为空");
			return map;
		}
		if (newPass == null || newPass.equals("")) {
			map.put("code", 400);
			map.put("msc", "密码为空");
			return map;
		}
		if (ondDlpass == null || ondDlpass.equals("")) {
			map.put("code", 400);
			map.put("msc", "密码为空");
			return map;
		}
		JC_ZhDB jcdb = jcDao.getJC_ZhByDlmc(Dlmc);
		if (jcdb == null) {
			map.put("code", 402);
			map.put("msc", "用户不存在");
			return map;
		}
		if (jcdb.getDlmc().equals(Dlmc) && jcdb.getDlpass().equals(ondDlpass)) {
			jcdb = new JC_ZhDB();
			jcdb.setDlpass(newPass);
			jcdb.setDlmc(Dlmc);
			if (jcDao.updateJC_ZhDB(jcdb) > 0) {
				map.put("code", 200);
				map.put("msc", "修改成功");
				return map;
			} else {
				map.put("code", 222);
				map.put("msc", "修改失败");
				return map;
			}
		} else {
			map.put("code", 300);
			map.put("msc", "原密码错误");
			return map;

		}
	}
}
